package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R
import kotlin.math.cos
import kotlin.math.sin

/*
* NetworkDiagramView
* */
class NetworkDiagramView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeJoin = Paint.Join.MITER
            strokeWidth = context.resources.getDimension(R.dimen.dp_1)
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_5F8F13)
        }
    }

    private val edgeArr = intArrayOf(1, 3, 5, 7, 9, 11)

    val minus = 0.0
    private val degreeArr = doubleArrayOf(60.0 - minus, 120.0 - minus * 2, 180.0 - minus * 3, 240.0 - minus * 4, 300.0 - minus * 5, 360.0 - minus * 6)
    //private val degreeArr = doubleArrayOf(60.0, 120.0, 180.0, 240.0, 300.0, 360.0)

    private val pointList = mutableListOf<EdgePoint>()

    init {
        for (i in 0.until(degreeArr.size * edgeArr.size)) {
            pointList.add(EdgePoint())
        }
    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            translate(cx, cy)
            mPath.apply {
                val fraction = 12
                var index = 0
                for (i in edgeArr.indices) {
                    val x0 = cx * edgeArr[i].toFloat() / fraction * Math.cos(Math.toRadians(360.0)).toFloat()
                    val y0 = cx * edgeArr[i].toFloat() / fraction * Math.sin(Math.toRadians(360.0)).toFloat()
                    moveTo(x0, y0)
                    val baseLength = cx * edgeArr[i].toFloat() / fraction
                    for (j in degreeArr.indices) {
                        val radius = Math.toRadians(degreeArr[j])
                        val x1 = baseLength * cos(radius).toFloat()
                        val y1 = baseLength * sin(radius).toFloat()
                        lineTo(x1, y1)

                        pointList[index].x = x1
                        pointList[index].y = y1
                        index++
                    }
                    close()
                }
            }
            drawPath(mPath, mPaint)

            mPath.apply {
                reset()
                for (i in edgeArr.indices) {
                    mPath.moveTo(pointList[i].x, pointList[i].y)
                    for (j in 1.until(edgeArr.size)) {
                        mPath.lineTo(pointList[j * 6 + i].x, pointList[j * 6 + i].y)
                    }
                }
            }
            drawPath(mPath, mPaint)
        }
    }

    data class EdgePoint(var x: Float = 0F, var y: Float = 0F)
}